package com.graduation.mathematics.db;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.graduation.mathematics.R;
import com.graduation.mathematics.bean.FunnyExam;
import com.graduation.mathematics.bean.UserInfo;

import org.litepal.LitePal;

import java.util.List;
import java.util.Random;


/**
 * <pre>
 *     author : Lucien Z
 *     e-mail : 825038797@qq.com
 *     time   : 2019/10/30
 *     desc   : 管理所有的数据库操作,包括初始化数据库，更新数据库，删除数据库
 *     version: 1.0
 * </pre>
 */
public class BaseDBHelper {
    private static final String TAG = BaseDBHelper.class.getSimpleName();
    private static final String IS_FIRST_INIT = "IS_FIRST_INIT";
    private volatile static BaseDBHelper sDataBaseHelper;

    public static BaseDBHelper getInstance() {
        if (sDataBaseHelper == null) {
            synchronized (BaseDBHelper.class) {
                if (sDataBaseHelper == null) {
                    sDataBaseHelper = new BaseDBHelper();
                }
            }
        }
        return sDataBaseHelper;
    }

    public int getLitePalDBVersion() {
        return LitePal.getDatabase().getVersion();
    }

    public boolean isFirstInit() {
        boolean isFirstInit = SPUtils.getInstance().getBoolean(IS_FIRST_INIT, true);
        if (isFirstInit) {
            SPUtils.getInstance().put(IS_FIRST_INIT, false);
            return true;
        }
        return false;
    }


    public List<UserInfo> getAllValidUsers() {
        return LitePal.findAll(UserInfo.class);
    }

    String[] points = new String[]{"知识点1", "知识点2", "知识点3", "知识点4", "知识点5", "知识点6", "知识点7"
            , "知识点8", "知识点9", "知识点10", "知识点11", "知识点12"};
    Random random = new Random();
    public void initFunnyExam(Context context){
        String[] funnyExamArray = context.getResources().getStringArray(R.array.funnyexams);
        for (int i = 0; i < funnyExamArray.length; i++) {
            FunnyExam funnyExam = new FunnyExam();
            funnyExam.setQuestionId( i + 1);
            funnyExam.setQuestion(funnyExamArray[i]);
            funnyExam.setKnowledgePoint(points[random.nextInt(11)]);
            funnyExam.setDegree(random.nextInt(5));
            funnyExam.save();
        }
    }
}